package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.ProgramCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.ProgramPageVO;
import com.xiaoshuidi.cloud.module.member.mapper.programarticle.MemberProgramInfoMapper;
import com.xiaoshuidi.cloud.module.member.pojo.programarticle.MemberProgramInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberProgramInfoRepository extends ServiceImpl<MemberProgramInfoMapper, MemberProgramInfo> {
    private final MemberProgramInfoMapper mapper;

    public Boolean create(ProgramCreateVO vo) {
        MemberProgramInfo memberProgramInfo = new MemberProgramInfo();
        BeanUtils.copyProperties(vo, memberProgramInfo);
        Long deptId = SecurityFrameworkUtils.getLoginUser().getDeptId();
        memberProgramInfo.setDeptId(deptId);
        mapper.insert(memberProgramInfo);
        return true;
    }

    public Page<MemberProgramInfo> getPage(ProgramPageVO pageVo) {
        Page<MemberProgramInfo> page = Page.of(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<MemberProgramInfo> wrapperX = new LambdaQueryWrapperX<MemberProgramInfo>()
                .geIfPresent(MemberProgramInfo::getCreateTime, pageVo.getBeginTime())
                .leIfPresent(MemberProgramInfo::getCreateTime, pageVo.getEndTime())
                .eqIfPresent(MemberProgramInfo::getProgramType, pageVo.getProgramType())
                .likeIfPresent(MemberProgramInfo::getName, pageVo.getName())
                .orderByDesc(MemberProgramInfo::getWeight)
                .orderByDesc(MemberProgramInfo::getCreateTime);
        return this.page(page, wrapperX);
    }
}
